package com.blue.gateway.paycore.alipay.pay;

import com.blue.gateway.common.Request;
import com.blue.gateway.common.response.PayResponse;
import com.blue.gateway.config.AliConfig;
import com.blue.gateway.config.BaseChannelConfig;
import com.blue.gateway.dto.Result;
import com.blue.gateway.dto.ResultWrapper;
import com.blue.gateway.enums.ResultCodeEnum;
import com.blue.gateway.utils.BigDecimalUtil;
import com.blue.gateway.common.request.PayRequest;
import com.blue.gateway.paycore.AbstractBaseChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author xiaoyiming
 * @date 2020/5/3 19:15
 * @depiction 支付宝支付接口 https://opendocs.alipay.com/apis/api_1/alipay.trade.create
 */
public abstract class AliPayService extends AbstractBaseChannelService {

    @Autowired
    private AliConfig aliConfig;

    @Override
    public Result validate(Request request) {
        PayRequest payRequest = (PayRequest) request;
        if (StringUtils.isBlank(payRequest.getNotifyUrl())) {
            return ResultWrapper.fail(ResultCodeEnum.NOTIFY_URL_IS_NULL);
        }
        if (StringUtils.isBlank(payRequest.getOutTradeNo())) {
            return ResultWrapper.fail(ResultCodeEnum.OUT_TRADE_NO_IS_NULL);
        }
        if (BigDecimalUtil.ltZeroOrNull(payRequest.getTotalAmount())) {
            return ResultWrapper.fail(ResultCodeEnum.TOTAL_AMOUNT_IS_NULL);
        }
        if (StringUtils.isBlank(payRequest.getSubject())) {
            return ResultWrapper.fail(ResultCodeEnum.SUBJECT_IS_NULL);
        }
        return ResultWrapper.success();
    }

    @Override
    public BaseChannelConfig getConfig(Request request) {
        return aliConfig;
    }

    @Override
    public String generateParams(Request request, BaseChannelConfig channelConfig) {
        return null;
    }

    //https://openapi.alipay.com/gateway.do?app_id=2018062960540016&biz_content=%7B%22out_trade_no%22%3A%222789875498798133%22%2C%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%2C%22subject%22%3A%22%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%8B%22%2C%22total_amount%22%3A%220.01%22%7D&charset=UTF-8&method=alipay.trade.page.pay&notify_url=http%3A%2F%2Fvqcjt4.natappfree.cc%2Fnotify_ali&return_url=http%3A%2F%2Fvqcjt4.natappfree.cc%2Freturn&sign=OlzqF6J1yZYC%2FC0zgZ5FpnpjEULyjil3e3NjH%2FKloOg9XtRBXi0%2B2H4zd7QKLlHIerICXFPVaN5YssR%2BsYIVge%2FY5DZZ4O392N3nrl0HpzrxDhkf5Wll46ohYT3dCbf4PZOMuYfub54wgg%2F%2BiDhvsVPd0xevBX9y57IuZrarFNg4my7iAZZ4DGEtcw34rLYBqXZeMWkWCh%2FhFRf5LYMuKEy1ex161xLKlOHvgYc%2FA0mJvruAndIP8a7e5da9%2FurK06uO%2Bk5aZvm1aeT8aCNzHBPqhOYueubIWG%2BDtHtTuKLoTJHRSPuSRqbFQ72yUPIOOgzCZjgShX1A54LuHeIyaw%3D%3D&sign_type=RSA2&timestamp=2020-05-05+18%3A03%3A44&version=1.0
    @Override
    public String execute(String requestParams, BaseChannelConfig channelConfig) {
        return requestParams;
    }

    @Override
    public Result analysisResult(String executeResult) {
        PayResponse response = new PayResponse();
        response.setPayUrl(executeResult);
        return ResultWrapper.success(response);
    }

    /** returnUrl
     * {
     "charset":[
     "UTF-8"
     ],
     "out_trade_no":[
     "2789875498798133"
     ],
     "method":[
     "alipay.trade.page.pay.return"
     ],
     "total_amount":[
     "0.01"
     ],
     "sign":[
     "TQ5owJVN5ULJhXo0KcIPOmouKI5iz+SmZ/QyhvBCS5N/8cAv6Eu8jm5vy7ySDE+Vzc7PU0kCFCiZ8eOMsJT1N2XF9JfAZ1g4lZCVG08hyoNjcMCrQhsuhH+R8Y+XGYyuAxCosStXE4gr7Sy7A4JeEBB//9smfko5AZ328tMfWSkvteoki9MK5IJ1hCupv+FInJuQChlGSZJl1jdnz9rASC0ACOoOY6c9spleXx0IEEMq97BzTTNlhCMBu3R4pqlKBWrLuDsECGlL5TI5ZxF4VPszL9uqsViebVqzsXzJk8qG8lR30GO9pfRcj9FYRv8rrdqDlRaCDwZI4l+qslSSJg=="
     ],
     "trade_no":[
     "2020050522001438201415885909"
     ],
     "auth_app_id":[
     "2018062960540016"
     ],
     "version":[
     "1.0"
     ],
     "app_id":[
     "2018062960540016"
     ],
     "sign_type":[
     "RSA2"
     ],
     "seller_id":[
     "2088721207163940"
     ],
     "timestamp":[
     "2020-05-05 18:04:57"
     ]
     }
     */
}
